<template>
  <div class="login">
    <div class="login-box">
      <div class="login-welcome">
        <div class="info">
          欢迎登录
        </div>
        <h3 class="title">
          {{ title }}
        </h3>
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <template #prefix>
              <svg-icon icon-class="user" class="el-input__icon input-icon" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <svg-icon icon-class="password" class="el-input__icon input-icon" />
            </template>
          </el-input>
        </el-form-item>


        <el-form-item v-if="captchaOnOff" prop="code">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <svg-icon icon-class="validCode" class="el-input__icon input-icon" />
            </template>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" class="login-code-img" @click="getCode" alt="" />
            <svg-icon v-if="!codeUrl" loading icon-class="loading" />
          </div>
        </el-form-item>


        <el-checkbox v-model="loginForm.rememberMe">
          记住密码
        </el-checkbox>
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            round
            type="primary"
            style="width:80%;"
            @click.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <!--          <div v-if="register" style="float: right;">-->
          <!--            <router-link class="link-type" :to="'/register'">立即注册</router-link>-->
          <!--          </div>-->
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2024-2025 xingluo.online All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import Cookies from '@/utils/xl-cookie.js'
import { decrypt, encrypt } from '@/utils/index.js'
import { md5 } from 'js-md5'
import store from '@/store/index.js'

const userStore = store.user
const route = useRoute()
const router = useRouter()
const loading = ref(false)
const title = import.meta.env.VITE_APP_TITLE
const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: false,
  code: '',
  uuid: ''
})
const codeUrl = ref('')

const loginFormRef = ref()
const loginRules = reactive({
  username: [
    {required: true, trigger: 'blur', message: '请输入您的账号'}
  ],
  password: [
    {required: true, trigger: 'blur', message: '请输入您的密码'}
  ],
  code: [
    {required: true, trigger: 'change', message: '请输入验证码'}
  ]
})


const captchaOnOff = ref(true)

function getCode(retry) {
  $xl.apis.getCaptchaImage().then(res => {
    captchaOnOff.value = res.captchaOnOff === undefined ? true : captchaOnOff.value
    if (captchaOnOff.value) {
      loginForm.uuid = res.uuid
      codeUrl.value = res.img
    }
  }).catch(err => {
    if (!retry) $xl.modal.msgError(err)
    setTimeout(() => {
      getCode("retry")
    }, 2000)
  })
}

getCode()

const username = Cookies.get('username')
const password = Cookies.get('password')
const rememberMe = Cookies.get('rememberMe')
loginForm.username = username === undefined ? loginForm.username : username
loginForm.password = password === undefined ? loginForm.password : decrypt(password)
loginForm.rememberMe = rememberMe === undefined ? false : Boolean(rememberMe)

function handleLogin() {
  loginFormRef.value.validate(valid => {
    if (valid) {
      loading.value = true
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.rememberMe) {
        Cookies.set("username", loginForm.username, {expires: 30})
        Cookies.set("password", encrypt(loginForm.password), {expires: 30})
        Cookies.set("rememberMe", loginForm.rememberMe, {expires: 30})
      } else {
        // 否则移除
        Cookies.remove("username")
        Cookies.remove("password")
        Cookies.remove("rememberMe")
      }
      userStore.login({
        username: loginForm.username,
        password: md5(loginForm.password),
        code: loginForm.code,
        uuid: loginForm.uuid
      }).then(() => {
        loading.value = false
        router.push({path: route.query.redirect || '/'})
      }).catch((err) => {
        $xl.modal.msgError(err)
        loading.value = false
        if (captchaOnOff.value) {
          getCode()
        }
      })
    }
  })
}

</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("@/assets/images/login-bg.gif");
  background-size: cover;
}

.login-box {
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.5);
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  width: 1000px;
  height: 500px;
  display: flex;
}

.login-welcome {
  width: 500px;
  padding-left: 80px;
  color: #FFFFFF;
  display: flex;
  flex-direction: column;
  justify-content: center;

  .info {
    font-size: 38px;
    font-weight: 800;
  }

  .title {
    font-size: 26px;
    font-weight: 800;
  }
}

.login-form {
  border-radius: 0 6px 6px 0;
  background: #ffffff;
  width: 500px;
  padding: 110px 60px 5px 40px;

  :deep(.el-input__wrapper) {
    box-shadow: unset;
    border-bottom: 1px solid #f1f1f1;
  }

  :deep(.el-input) {
    height: 42px;
    font-size: 14px;

    input {
      height: 42px;
      border: unset;
      border-radius: 0;
      padding-left: 16px;
    }
  }

  .input-icon {
    height: 39px;
    width: 16px;
    margin-left: 12px;
  }

  .el-checkbox {
    height: 32px;
    margin: 0 0 25px 0;
  }

  .el-button {
    height: auto;
    font-size: 16px;
    padding: 10px 20px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
